"""
系统初始化模块
使用了资源配置、挂载点管理、线程池设置和事务日志恢复等功能，以确保文件系统的稳定性和性能。
"""
#include <dokan.h>
#include <iostream>
#include <windows.h>
#include <map>
#include <string>
#include <fstream>

// 资源限制结构体
struct ResourceLimits {
    int maxCPU;
    int maxMemoryMB;
};

// 全局资源限制配置
ResourceLimits g_resourceLimits = {2, 2048}; // 2vCPU, 2GB RAM

// 初始化资源限制
void InitializeResourceLimits() {
    std::cout << "Initializing resource limits: "
              << g_resourceLimits.maxCPU << " vCPU & "
              << g_resourceLimits.maxMemoryMB << " MB RAM" << std::endl;

    // 限制CPU核心数
    SetThreadAffinityMask(GetCurrentThread(), 1);

    // 限制工作集大小
    SetProcessWorkingSetSize(GetCurrentProcess(), 
                             (SIZE_T)(1024 * 1024), // Minimum working set
                             (SIZE_T)(1024 * 1024 * g_resourceLimits.maxMemoryMB / 2)); // Maximum working set
}

// 挂载点配置
const std::wstring DEFAULT_MOUNT_POINT = L"F:";

// 设置挂载点
std::wstring GetMountPoint(int argc, char* argv[]) {
    std::wstring mountPoint;

    if (argc > 1) {
        // 从命令行参数获取挂载点
        mountPoint.assign(argv[1], argv[1] + strlen(argv[1]));
    } else {
        mountPoint = DEFAULT_MOUNT_POINT;
    }

    return mountPoint;
}

// 检测挂载点冲突
bool IsMountPointAvailable(std::wstring mountPoint) {
    DWORD attributes = GetFileAttributes(mountPoint.c_str());
    if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
        return true; // 挂载点可用
    }
    return false;
}

// 错误处理
void HandleError(int errorCode) {
    static std::map<int, std::wstring> errorMessages = {
        {ERROR_ALREADY_EXISTS, L"Mount point is already in use."},
        {ERROR_ACCESS_DENIED, L"Access denied to the mount point."},
        {ERROR_INVALID_PARAMETER, L"Invalid parameters provided."}
    };

    auto it = errorMessages.find(errorCode);
    if (it != errorMessages.end()) {
        std::wcout << L"Error: " << it->second << std::endl;
    } else {
        std::wcout << L"Unknown error: " << errorCode << std::endl;
    }
}

// 日志记录
void Log(const std::wstring& message) {
    std::wofstream logFile(L"dokanfs.log", std::ios_base::app);
    logFile << GetTickCount() << L" - " << message << std::endl;
}

// 初始化Dokan文件系统
int main(int argc, char* argv[]) {
    // 初始化资源限制
    InitializeResourceLimits();

    // 获取挂载点
    std::wstring mountPoint = GetMountPoint(argc, argv);
    if (!IsMountPointAvailable(mountPoint)) {
        std::wcout << L"Mount point " << mountPoint << L" is not available." << std::endl;
        return -1;
    }

    // 配置Dokan选项
    const DOKAN_OPTIONS dokanOptions = {
        DOKAN_OPTIONS_DEFAULT,
        1,
        mountPoint.c_str(),
        0,
        DOKAN_EVENT_TIMEOUT,
        DOKAN_MAJOR_VERSION,
        DOKAN_MINOR_VERSION
    };

    // 日志记录初始化
    Log(L"Dokan file system initialization started.");

    // 初始化Dokan文件系统
    int result = DokanMain(&dokanOptions, &dokanOperations);
    if (result != DOKAN_SUCCESS) {
        HandleError(result);
        Log(L"Dokan file system initialization failed.");
        return -1;
    }

    // 日志记录成功信息
    Log(L"Dokan file system started successfully.");

    return 0;
}